#coding=utf8
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')  
from pdfminer.pdfparser import PDFParser, PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import *

def findStrs(text, keys):
    for key in keys:
        if text.find(key)!=-1:
            return 1
    return 0
    
    
if __name__ == '__main__': 
    # 4397101617528 苏道波
    # 1950101722790 邵丹
    keys=['4397101617528','1950101722790']
    fp = open(r'd:\1.pdf', 'rb')
    parser = PDFParser(fp)
    doc = PDFDocument()
    parser.set_document(doc)
    doc.set_parser(parser)
    doc.initialize()
    
    if not doc.is_extractable:
        raise PDFTextExtractionNotAllowed
    
    rsrcmgr = PDFResourceManager()
    
    laparams = LAParams()
    device = PDFPageAggregator(rsrcmgr, laparams=laparams)
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    for i, page in enumerate(doc.get_pages()):
        interpreter.process_page(page)
        layout = device.get_result()

        for x in layout:
            if(isinstance(x, LTTextBoxHorizontal)):
                for textline in x:
                    text = textline.get_text()
                    if findStrs(text,keys):
                        print 'Page', i+1, ':' ,
                        print text 
                        
                        
    print 'end', i+1

    